// 检查语法：1、空行不能多； 2、注释需要 //空格；  3、key:空格value,空格key：空格value   不然会拼命报错

import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import ListGoods from '../components/goods/ListGoods.vue'
import AddListGoods from '../components/goods/AddListGoods.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'




Vue.use(VueRouter)
const routes = [
  {
    // 重定向redirect，访问/根目录就自动跳转到/login登录页面
    path: '/', redirect: '/login'
  },
  {
    // 访问 根地址/login 就可以进去这个组件 （）
    path: '/login', component: Login
  },
  {
    path: '/home', component: Home,
    // 重定向redirect，成功登录/home页面就自动跳转到/users页面
    redirect:'/users',
    // 子路由
    children:[
      {
        path: '/welcome',component: Welcome
      },
      {
        path:'/users',component: Users
      },
      {
        path:'/rights',component: Rights
      },
      {
        path:'/roles',component: Roles
      },
      {
        path:'/categories',component: Cate
      },
      {
        path:'/params',component:Params
      },
      {
        path:'/goods',component:ListGoods
      },
      {
        path:'/add_list_goods',component:AddListGoods
      },
      {
        path:'/orders',component:Order
      },
      {
        path:'/reports',component:Report
      }

    ],

  }
  
]

const router = new VueRouter({
  routes
});

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
/*
  to：将要访问的路径  to.path
  from：代表从哪个路径跳转过来的
  next：是一个放行函数 next() ，强制跳转函数 next(强制跳转路径)
*/
  if(to.path === '/login') return next();
  // 获取前面Login组件登录成功后保存的token
  const tokenStr = window.sessionStorage.getItem("token");
  if( !tokenStr ) return next('/login');

  next();

});

export default router
